package com.blast.comicsisland.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.blast.comicsisland.R;
import com.blast.comicsisland.utils.RequestManager;
import com.blast.comicsisland.utils.XUtilsImageLoader;
import com.lidroid.xutils.BitmapUtils;

import java.util.HashMap;
import java.util.List;

/**
 * Created by ibm on 2015/6/25.
 */
public class TopicAdapter extends BaseAdapter {
    private Context context;
    private List<HashMap<String, String>> list;
    private BitmapUtils bitmapUtils;

    /*private RequestQueue requestQueue;*/
    private XUtilsImageLoader imageLoader;

    public TopicAdapter(Context context, List<HashMap<String, String>> list){
        this.context = context;
        this.list = list;
        imageLoader = new XUtilsImageLoader(context);


        //RequestManager.getInstance().getRequestQueue();
        //======================
        //imageLoader = RequestManager.getInstance().getImageLoader();
        //===========================
        /*if (context != null){
            requestQueue = Volley.newRequestQueue(context);
            imageLoader = new ImageLoader(
                    requestQueue,
                    new ImageLoader.ImageCache() {
                        private LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(20);
                        @Override
                        public Bitmap getBitmap(String url) {
                            return cache.get(url);
                        }

                        @Override
                        public void putBitmap(String url, Bitmap bitmap) {
                            cache.put(url, bitmap);
                        }
                    }
            );
        }*/
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;

        if (convertView == null){
            ret = LayoutInflater.from(context).inflate(R.layout.item_topic, parent, false);
        }else {
            ret = convertView;
        }

        ViewHolder holder = (ViewHolder)ret.getTag();
        if (holder == null){
            holder = new ViewHolder();
            holder.imageView = (ImageView) ret.findViewById(R.id.topic_img);
            holder.textView = (TextView) ret.findViewById(R.id.topic_txt);
            ret.setTag(holder);
        }


        String url = list.get(position).get("coverurl");
        String name = list.get(position).get("name");


        if (url != null && !url.equals("")) {

            //holder.imageView.setDefaultImageResId(R.drawable.imgbg);
            //holder.imageView.setErrorImageResId(R.drawable.ic_launcher);
            //holder.imageView.setImageUrl(url, imageLoader);
            imageLoader.display(holder.imageView,url);
        }

        holder.textView.setText(name);
        return ret;
    }

    private static class ViewHolder{
        public ImageView imageView;
        public TextView textView;
    }
}
